#include "mainwindow.h"
#include <QHBoxLayout>
#include "easyAI.h"
#include "hardAI.h"
#include "netgame.h"
#include "buttonpanel.h"
#include "choosedlg.h"
#include <QMessageBox>

MainWindow::MainWindow( QWidget *parent) : QWidget(parent)
{
    ChooseDlg dlg;
    dlg.exec();
    startGame(dlg.selected);
}

void MainWindow::startGame(int gameType)
{
    QFont font("Arial", 16, QFont::Bold); //设置字体的类型、大小、粗细

    QLabel*  labelTimer=new QLabel(this);
    labelTimer->setText("Time:");
    labelTimer->setFont(font); //将字体应用到QLabel上
    labelTimer->setGeometry(QRect(580, 60, 91, 51));

    lcdTimer = new QLCDNumber(this);
    lcdTimer->setGeometry(QRect(660, 60, 91, 51));
    lcdTimer->setDigitCount(2);//显示位数
    lcdTimer->setMode(QLCDNumber::Dec);//设置显示模式为十进制
    lcdTimer->setSegmentStyle(QLCDNumber::Flat);//设置显示外观Outline、Filled、Flat三种
    lcdTimer->display("60");

    QFont font1("Arial", 12, QFont::Normal); //设置字体的类型、大小、粗细
    QLabel*  labelRedStep=new QLabel(this);
    labelRedStep->setText("Red Step:");
    labelRedStep->setFont(font1); //将字体应用到QLabel上
    labelRedStep->setGeometry(QRect(580, 130, 91, 51));

    lcdRed = new QLCDNumber(this);
    lcdRed->setGeometry(QRect(660, 130, 70, 40));
    lcdRed->setDigitCount(2);//显示位数
    lcdRed->setMode(QLCDNumber::Dec);//设置显示模式为十进制
    lcdRed->setSegmentStyle(QLCDNumber::Outline);//设置显示外观Outline、Filled、Flat三种
    lcdRed->display("00");

    QLabel*  labelBlackStep=new QLabel(this);
    labelBlackStep->setText("Black Step:");
    labelBlackStep->setFont(font1); //将字体应用到QLabel上
    labelBlackStep->setGeometry(QRect(580, 180, 91, 51));

    lcdBlack = new QLCDNumber(this);
    lcdBlack->setGeometry(QRect(660, 180, 70, 40));
    lcdBlack->setDigitCount(2);//显示位数
    lcdBlack->setMode(QLCDNumber::Dec);//设置显示模式为十进制
    lcdBlack->setSegmentStyle(QLCDNumber::Outline);//设置显示外观Outline、Filled、Flat三种
    lcdBlack->display("00");

    timer = new QTimer;
    timer->setInterval(1000);
    m_secCnt = 0;
    connect(timer,SIGNAL(timeout()),this,SLOT(updateTime()));

    QHBoxLayout* hLay = new QHBoxLayout(this);
    ButtonPanel* buttonPanel = new ButtonPanel;

    if (gameType == 0) {
        board = new EasyAI(this);
    } else if (gameType == 1) {
        board = new HardAI(this);
    }  else if (gameType == 2) {
        board = new NetGame(this,true);
    } else if (gameType == 3) {
        board = new NetGame(this,false);
    }else{
        exit(0);
    }
    hLay->addWidget(board, 1);
    connect(buttonPanel, SIGNAL(clickUndo()), board, SLOT(undoBoard()));
    connect(buttonPanel, SIGNAL(clickReset()), board, SLOT(resetBoard()));
    connect(buttonPanel, SIGNAL(clickBack()), board,  SLOT(backGame()));
    connect(this, SIGNAL(timeoutEvent()), this,  SLOT(timeout()));
    hLay->addWidget(buttonPanel);



    setWindowFlags(windowFlags()&~Qt::WindowMinMaxButtonsHint|Qt::WindowMinimizeButtonHint);
}

void MainWindow::resetTimer(bool isReset)
{
    if(isReset){
        m_secCnt=0;
    }

    timer->start();
}

void MainWindow::pauseTimer()
{
    if(timer->isActive()){
        m_secCnt=0;
        timer->stop();
    }
}

void MainWindow::updateStep(bool isRed,int step)
{
    QString sec = QString::number(step);
    if(sec.length() == 1) sec = "0" + sec;
    if(isRed){
        lcdRed->display(sec);
    }else{
        lcdBlack->display(sec);
    }
}

void MainWindow::switchGame(int gameType)
{
    if (gameType == 0) {
        board = new EasyAI(this);
    } else if (gameType == 1) {
        board = new HardAI(this);
    }  else if (gameType == 2) {
        board = new NetGame(this,true);
    } else if (gameType == 3) {
        board = new NetGame(this,false);
    }
}

void MainWindow::updateTime(){
    m_secCnt++;

    if(m_secCnt > 60) {
        timer->stop();
        m_secCnt = 0;
        QMessageBox::information(this,
                                 tr("information"),tr("Timeout,Game Over"),
                                 QMessageBox::Ok,
                                 QMessageBox::Ok);
        board->init();
        resetTimer(true);
        //        emit timeoutEvent(10);
    }
    QString sec = QString::number(60 - m_secCnt);
    if(sec.length() == 1) sec = "0" + sec;
    lcdTimer->display(sec);
}

